package com.zhuzhumail.utils;


import com.zhuzhumail.entity.AttendanceCalendarVo;
import com.zhuzhumail.entity.KqAbsenceDutyVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class KqClockUtils {
    public  static List<AttendanceCalendarVo> queryAllAbsenceStatusUtils(){
        //先获取3个月的开始的月份
        int acquisitionTime = io.kqoa.modules.kq.utils.TimeUtils.queryMonthAcquisitionTime(io.kqoa.modules.kq.utils.TimeUtils.queryGetFirstMonths());
        int [] ints=new int[3];
        ints[0] = acquisitionTime;
        ints[1] = acquisitionTime+1;
        ints[2] = acquisitionTime+2;
        Map<Integer,  KqAbsenceDutyVo []> integerListMap=new HashMap<>();

        for (int i = 0; i <ints.length; i++) {
            KqAbsenceDutyVo [] kqAbsenceDutyVos1=new KqAbsenceDutyVo[32];
            //根据月份获取开始的时间和结束的时间
            List<String> dateList = io.kqoa.modules.kq.utils.TimeUtils.queryStartTimesMonth(ints[i]);
            SimpleDateFormat sdf = new   SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            long time = 0;
            try {
                //每一个开始的时间
                calendar.setTime(sdf.parse(dateList.get(0)));
                //每一个月结束的时间
                time= sdf.parse(dateList.get(1)).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //从开始时间遍历到结束的时间
            for (long d = calendar.getTimeInMillis(); d <= time; d = io.kqoa.modules.kq.utils.TimeUtils.get_d_plaus_1(calendar)) {
                String format = sdf.format(d);
                KqAbsenceDutyVo kqAbsenceDutyVo=new  KqAbsenceDutyVo();
                //获取今天的时间
                kqAbsenceDutyVo.setWorkTime(io.kqoa.modules.kq.utils.TimeUtils.convertStringData(format));
                //将日传递进去
                kqAbsenceDutyVo.setAttendanceDay(io.kqoa.modules.kq.utils.TimeUtils.GetsDayTime(io.kqoa.modules.kq.utils.TimeUtils.convertStringData(format)));
                //获取周几
                kqAbsenceDutyVo.setWeek(io.kqoa.modules.kq.utils.TimeUtils.getWeekday(io.kqoa.modules.kq.utils.TimeUtils.convertStringData(format)));
                //号作为元素的索引
                kqAbsenceDutyVos1[io.kqoa.modules.kq.utils.TimeUtils.GetsDayTime(io.kqoa.modules.kq.utils.TimeUtils.convertStringData(format))-1]=kqAbsenceDutyVo;
            }
            //将每一个月和对应的日加入map中  key是月 values是对应的日
            integerListMap.put(ints[i],kqAbsenceDutyVos1);
        }
        List<AttendanceCalendarVo> calendarVoList=new ArrayList<>();
        //进行加空的操作  前面是月份 后面是日
        integerListMap.forEach((integer, kqAbsenceDutyVos) -> {
          AttendanceCalendarVo attendanceCalendarVo = new AttendanceCalendarVo();
            attendanceCalendarVo.setMonth(integer);
            //去调null
            KqAbsenceDutyVo[] absenceDutyVos = ArrayUtils.deleteArrayNull(kqAbsenceDutyVos);
            //根据枚举判断应该空几格
            System.out.println("======================"+absenceDutyVos[0].getWeek());
            int attendanceEunm = io.kqoa.modules.kq.entity.enums.CalendarAttendanceEunm.queryCalendarAttendanceEunm(absenceDutyVos[0].getWeek());
            attendanceCalendarVo.setEmptySpace(attendanceEunm);
            System.out.println("应该null几格========================"+attendanceEunm);
            //创建一个新的数组
//            KqAbsenceDutyVo[] kqAbsenceDutyVos2 = new KqAbsenceDutyVo[absenceDutyVos.length+attendanceEunm];
//            for (int i = 0; i < absenceDutyVos.length; i++) {
//
//                //获取应该null几格
////                log.info("获取的星期"+kqAbsenceDutyVos1[i].getWeek());
//                kqAbsenceDutyVos2[i + attendanceEunm] = absenceDutyVos[i];
//
//            }
//            //            去掉数组的null 搞成{}  关键
//            for (int i = 0; i < kqAbsenceDutyVos2.length ; i++) {
//                if (kqAbsenceDutyVos2[i]==null){
//                    KqAbsenceDutyVo kqAbsenceD=new KqAbsenceDutyVo();
//                    kqAbsenceD.setIsnull(1);
//                    kqAbsenceDutyVos2[i] =kqAbsenceD;
//                }
//            }
            KqAbsenceDutyVo[] kqAbsenceDutyV = new  KqAbsenceDutyVo[absenceDutyVos.length];
            //进行到插入
            int v=0;
            for (int i = absenceDutyVos.length-1; i >=0 ; i--) {
                kqAbsenceDutyV[v]=absenceDutyVos[i];
                v++;
            }
            //进行添加
            attendanceCalendarVo.setKqAbsenceDutyVos(kqAbsenceDutyV);
            //添加进去
            calendarVoList.add(attendanceCalendarVo);
        });

        return calendarVoList;
    }
}
